<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'operating:ftdsExamine:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('更新') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.examineSf }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsOperatingFtdsExamineList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { ftdsExamineDelete, ftdsExamineListData } from '/@/api/operating/ftdsExamine';
  import { ftdsExamineDisable, ftdsExamineEnable } from '/@/api/operating/ftdsExamine';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('operating.ftdsExamine');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('审核意见管理管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('省份'),
        field: 'examineSf',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'examineCjsj',
        component: 'Input',
      },
      {
        label: t('法院名称'),
        field: 'examineFymc',
        component: 'Input',
      },
      {
        label: t('案件名称'),
        field: 'examineAjmc',
        component: 'Input',
      },
      {
        label: t('案件金额'),
        field: 'examineAjje',
        component: 'Input',
      },
      {
        label: t('身份证'),
        field: 'examineSfz',
        component: 'Input',
      },
      {
        label: t('手机号码'),
        field: 'examineSjhm',
        component: 'Input',
      },
      {
        label: t('案件状态'),
        field: 'examineAjzt',
        component: 'Input',
      },
      {
        label: t('审核意见'),
        field: 'examineShyj',
        component: 'Input',
      },
      {
        label: t('所属账号'),
        field: 'examineSszh',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('省份'),
      dataIndex: 'examineSf',
      key: 'a.examine_sf',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('创建时间'),
      dataIndex: 'examineCjsj',
      key: 'a.examine_cjsj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('审核时间'),
      dataIndex: 'examineShsj',
      key: 'a.examine_shsj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('法院名称'),
      dataIndex: 'examineFymc',
      key: 'a.examine_fymc',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('案件描述'),
      dataIndex: 'examineAjms',
      key: 'a.examine_ajms',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('案件名称'),
      dataIndex: 'examineAjmc',
      key: 'a.examine_ajmc',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('案件金额'),
      dataIndex: 'examineAjje',
      key: 'a.examine_ajje',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('身份证'),
      dataIndex: 'examineSfz',
      key: 'a.examine_sfz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('手机号码'),
      dataIndex: 'examineSjhm',
      key: 'a.examine_sjhm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('案件状态'),
      dataIndex: 'examineAjzt',
      key: 'a.examine_ajzt',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: '',
      // 根据数据状态改变单元格的颜色（例子）
      customCell: (record: Recordable) => {
        var color = ''
        if (record.examineAjzt === '审核拒绝'){
            color='#d31730';
        }
        if (record.examineAjzt === '待补正材料'){
            color='#e2df0f';
        }
        if (record.examineAjzt === '待审核'){
            color='#0fe232';
        }
        if (record.examineAjzt === '审核通过'){
            color='#0f9fe2';
        }
        if (record.examineAjzt === '已立案'){
            color='#0f9fe2';
        }
        return {
          // innerHTML: record.testTextarea, // 原样输出，不进行html编码（例子）
          style: `background-color: ${color} !important`,
        };
      },
    },
    {
      title: t('审核意见'),
      dataIndex: 'examineShyj',
      key: 'a.examine_shyj',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('所属账号'),
      dataIndex: 'examineSszh',
      key: 'a.examine_sszh',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑审核意见管理'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'operating:ftdsExamine:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用审核意见管理'),
        popConfirm: {
          title: t('是否确认停用审核意见管理'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'operating:ftdsExamine:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用审核意见管理'),
        popConfirm: {
          title: t('是否确认启用审核意见管理'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'operating:ftdsExamine:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除审核意见管理'),
        popConfirm: {
          title: t('是否确认删除审核意见管理'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'operating:ftdsExamine:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: ftdsExamineListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/operating/ftdsExamine/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await ftdsExamineDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await ftdsExamineEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await ftdsExamineDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
